package com.yand.serviceimpl.business.order;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yand.api.entity.business.order.THfOrder;
import com.yand.api.entity.business.product.THfDiscountDetail;
import com.yand.api.entity.business.product.THfProductDetail;
import com.yand.api.service.business.order.OrderService;
import com.yand.api.util.IdUtils;
import com.yand.mapper.business.order.THfOrderMapper;
import com.yand.mapper.business.product.THfDiscountDetailMapper;
import com.yand.mapper.business.product.THfProductDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * 订单业务实现类
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private THfOrderMapper orderMapper;

    @Autowired
    private THfDiscountDetailMapper discountDetailMapper;


    /**
     * 查询所有的订单信息
     * @param order
     * @return
     */
    @Override
    public PageInfo<THfOrder> queryAllOrderList(THfOrder order) {
        PageHelper.startPage(order.getPageNum(), order.getPageSize());

        return PageInfo.of(orderMapper.selectAllOrderList(order));
    }

    @Override
    public THfOrder getOrderById(Integer id) {
        return orderMapper.selectByPrimaryKey(id);
    }

    @Override
    public String convertProductLevel(String grade) {
        String level = "";
        if(grade.equals("1") || grade.equals("2") ||grade.equals("3")){
            level = "小1~小3";
        }else{
            switch (grade) {
                case "4" :
                    level = "小4";
                break;
                case "5" :
                    level = "小5";
                    break;
                case "6" :
                    level = "小6";
                    break;
                case "7" :
                    level = "初一";
                    break;
                case "8" :
                    level = "初二";
                    break;
                case "9" :
                    level = "初三";
                    break;
                case "10" :
                    level = "高一";
                    break;
                case "11" :
                    level = "高二";
                    break;
                case "12" :
                    level = "高三";
                    break;
            }
        }

        return level;
    }

    /**
     * 添加到订单表中
     * @param order
     */
    @Override
    public void insertToOrder(THfOrder order) {
        String discountStr = order.getDiscountStr();
        THfDiscountDetail discountDetail = new THfDiscountDetail();
        discountDetail.setDiscountStr(discountStr);
        discountDetail = discountDetailMapper.selectBeanByDiscount(discountDetail);
        //得出折扣
        double discount = discountDetail.getDiscount();
        //课时单价*折扣
        String price = order.getExtand3();

        BigDecimal priceNew = new BigDecimal(price);
        BigDecimal discountNew = new BigDecimal(discount);
        BigDecimal actualPrice =priceNew.multiply(discountNew);
        BigDecimal payMoney = order.getPayMoney();
        //课时数
        BigDecimal classNum = payMoney.divide(actualPrice,2,BigDecimal.ROUND_HALF_UP);
        String orderId = IdUtils.createId("OE");
        order.setOrderId(orderId);
        order.setCreatetime(new Date());
        order.setSurplusMoney(payMoney);
        order.setTotalClassnum(classNum.intValue());
        order.setSurplusClassnum(classNum.intValue());
        order.setStatus(0);
        order.setDiscount(new BigDecimal(discount));
        orderMapper.insert(order);
    }

    @Override
    public void updateOrderStatus(String studentId, String status) {
        Map<String, Object> param = new HashMap<>();
        param.put("studentId",studentId);
        param.put("status",status);
        orderMapper.updateOrderStatus(param);
    }

    @Override
    public void updateOrder(THfOrder order) {
        orderMapper.updateByPrimaryKeySelective(order);
    }

    @Override
    public THfOrder getOrderByStudentId(Integer studentId) {
        Map<String, Object> param = new HashMap<>();
        param.put("studentId",studentId);
        return orderMapper.selectOrderByOther(param);
    }


}
